//
//  CustomRulerNewsView.m
//  AmountBody
//
//  Created by smm on 2018/1/8.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "CustomRulerNewsView.h"
#import "EssentialModel.h"
#import <ReactiveObjC/ReactiveObjC.h>

static const CGFloat View_Tag = 202000;
static const CGFloat Text_Tag = 201000;
static const CGFloat Image_Tag = 203000;
static const CGFloat button_Tag = 204000;

@implementation CustomRulerNewsView

-(void)loadRulerNewsViewForHead//有头部分类
{
    CGFloat top = 100; // 顶端盖高度
    CGFloat bottom = 20 ; // 底端盖高度
    CGFloat left = 100; // 左端盖宽度
    CGFloat right = 20; // 右端盖宽度
    CGFloat imageW = 524;
    CGFloat imageH = 641;
    
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    UIImageView * kimage = [[UIImageView alloc]init];
    
    kimage.tag = Image_Tag;
    UIImage *image = [UIImage imageNamed:@"kuang3"];
    // 指定为拉伸模式，伸缩后重新赋值
    image = [image stretchImageWithInsetsTop:top left:left bottom:bottom right:right];
    kimage.image = image;
    kimage.userInteractionEnabled  = YES;
    [self addSubview:kimage];
    [kimage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
    }];
    CGFloat jianGeW = 50;
    CGFloat viewW = imageW-20;
    UIView * headView = [[UIView alloc]init];
    [kimage addSubview:headView];
    [headView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(kimage).offset(-20);
        make.top.equalTo(@(top/2.0+10));
        make.height.equalTo(@(40));
        make.width.equalTo(@(180));
    }];
    
    [self loadButtonViewForHead:headView];
    [self loadUIForPublic:kimage andPointYP:top];
    [kimage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
}
-(void)loadButtonViewForHead:(UIView *)tmpView
{
    for (UIView * view in tmpView.subviews) {
        [view removeFromSuperview];
    }
    NSArray * nameArray = @[@"正常款",@"短款",@"长款"];
    CGFloat buttonW = 180/3.0;
    CGFloat buttonH = 35;
    UIButton * l_button = nil;
    for (int i=0; i<3; i++) {
        UIButton * button = [[UIButton alloc]init];//WithFrame:CGRectMake(buttonW*i, (40-buttonH)/2.0, buttonW, buttonH)
        [tmpView addSubview:button];
        [button mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(tmpView).offset(buttonW*i);
            make.width.equalTo(@(buttonW));
            make.top.equalTo(@((40-buttonH)/2.0));
            make.height.equalTo(@(buttonH));
        }];
        button.tag = button_Tag+i;
        [button setTitle:[nameArray objectAtIndex:i] forState:UIControlStateNormal];
        button.titleLabel.font = Cell_Medium_Font;
        [button setTitleColor:ZhongZi_Color forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonFunc:) forControlEvents:UIControlEventTouchUpInside];
        l_button = button;
    }
    UIButton * button = [tmpView viewWithTag:button_Tag];
    [button setTitleColor:RGBColorAlpha(255, 66, 89, 1) forState:UIControlStateNormal];
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = RGBColorAlpha(255, 66, 89, 1);
    [tmpView addSubview:_lineView];
    [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(tmpView);
        make.left.right.equalTo(button);
        make.height.mas_equalTo(1);
    }];
    
}
-(void)loadRulerNewsViewForNomal
{
    CGFloat top = 100; // 顶端盖高度
    CGFloat bottom = 20 ; // 底端盖高度
    CGFloat left = 100; // 左端盖宽度
    CGFloat right = 20; // 右端盖宽度
    CGFloat imageW = 524;
    CGFloat imageH = 641;
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    UIImageView * kimage = [[UIImageView alloc]init];
    
    kimage.tag = Image_Tag;
    UIImage *image = [UIImage imageNamed:@"kuang3"];
    // 指定为拉伸模式，伸缩后重新赋值
    image = [image stretchImageWithInsetsTop:top left:left bottom:bottom right:right];
    kimage.image = image;
    kimage.userInteractionEnabled  = YES;
    [self addSubview:kimage];
    [kimage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
    }];
    [self loadUIForPublic:kimage andPointYP:top];
    [kimage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
}

-(void)loadRulerNewsViewForSize
{
    CGFloat top = 100; // 顶端盖高度
    CGFloat bottom = 20 ; // 底端盖高度
    CGFloat left = 100; // 左端盖宽度
    CGFloat right = 20; // 右端盖宽度
    CGFloat imageW = 524;
    CGFloat imageH = 641;
    
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    
    UIImageView * kimage = [[UIImageView alloc]init];
    [self addSubview:kimage];
    [kimage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    UIImage *image = [UIImage imageNamed:@"kuang3"];
    // 指定为拉伸模式，伸缩后重新赋值
    image = [image stretchImageWithInsetsTop:top left:left bottom:bottom right:right];
    kimage.image = image;
    kimage.userInteractionEnabled  = YES;
    CGFloat jianGeW = 50;
    CGFloat t_viewH = 100;
    EssentialModel *model = [[EssentialModel alloc]init];
    model.name = @"号型选择：";
    model.value =@"155/82";
    model.essentialType = EssentialViewTypeForRulerWithSize;
    RulerShowView * headView = [[RulerShowView alloc]init];//WithFrame:CGRectMake(jianGeW, top, viewW, t_viewH)
     [kimage addSubview:headView];
    [headView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(kimage).offset(jianGeW);
        make.right.equalTo(kimage).offset(-30);
        make.top.equalTo(kimage).offset(top);
        make.height.equalTo(@(t_viewH));
    }];
    headView.valueText.delegate  =self;
    headView.valueText.tag = Text_Tag-1;
    headView.valueText.keyboardType = UIKeyboardTypeNumberPad;
    NSArray * array = [NSArray arrayWithObjects:@"155/82",@"160/66",@"160/75",@"160/80",@"160/84",@"160/86",@"165/90",@"165/94",@"165/98",@"170/90",@"170/92",@"170/96",@"180/100", nil];
    headView.scaleArray = array;
    [headView reloadUI:model];
    //        headView.rulerCustom.rulerDelegate = self;
    int index = 0;
    [headView.rulerCustom setValue:index animated:YES];
   
    [self loadUIForPublic:kimage andPointYP:top+t_viewH];
   
//    [kimage mas_updateConstraints:^(MASConstraintMaker *make) {
//
//        make.bottom.equalTo(self);
//    }];
    
}

-(void)loadUIForPublic:(UIView *)backView andPointYP:(CGFloat)point_y;
{
    NSMutableArray * needArr = [[NSMutableArray alloc]init];
    NSArray * nameArr = [NSArray arrayWithObjects:@"胸围：",@"腰围：",@"臀围：",@"颈围：",@"肩宽：", nil];
    NSArray * valueArr = [NSArray arrayWithObjects:@"10",@"40",@"170",@"70",@"60", nil];
    NSArray * unitArr = [NSArray arrayWithObjects:@"cm",@"cm",@"cm",@"cm",@"cm", nil];
    for (int i=0; i<nameArr.count; i++) {
        EssentialModel * model = [[EssentialModel alloc]init];
        model.name = [nameArr objectAtIndex:i];
        model.value = [valueArr objectAtIndex:i];
        model.unit =[unitArr objectAtIndex:i];
        model._id = [NSString stringWithFormat:@"%d",i];
        [needArr addObject:model];
    }
    
    CGFloat t_viewH = 80;
    CGFloat jianGeW = 85;
    RulerShowView * l_view = nil;
    for ( int j=0; j<needArr.count; j++) {
        EssentialModel *model = [needArr objectAtIndex:j];
        model.essentialType = EssentialViewTypeForRulerWithNomal;
        RulerShowView * view = [[RulerShowView alloc]init];
        [backView addSubview:view];
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backView).offset(50);
            make.right.equalTo(backView).offset(-30);
            make.top.equalTo(backView).offset(point_y+t_viewH*j);
            make.height.equalTo(@(t_viewH));
        }];
        view.tag = View_Tag+j;
        view.valueText.delegate  =self;
        view.valueText.tag = Text_Tag+j;
        [view reloadUI:model];
        if (j%2==1) {
            view.rulerCustom.min = 0;
            view.rulerCustom.max = 160;
        }else{
            view.rulerCustom.min = 0;
            view.rulerCustom.max = 200;
        }
        [view.rulerCustom setValue:[model.value intValue] animated:YES];
        //在执行此方法前，可先设定参数：最小值，最大值，横向，纵向等等   ------若不设定，则按照默认值绘制
//        [view.rulerCustom customRulerWithLineColor:customColorMake(0, 0, 0) NumColor:ShenZi_Color scrollEnable:YES];
        
        
        l_view = view;
    }
    [l_view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(backView).offset(-40);
    }];
}

#pragma mark - 手势按钮方法
-(void)buttonFunc:(UIButton*)button
{
    for (UIButton * tmpButton in button.superview.subviews) {
        if ([tmpButton isKindOfClass:[UIButton class]]) {
            [tmpButton setTitleColor:ZhongZi_Color forState:UIControlStateNormal];
        }
        
    }
    [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(button.superview);
        make.left.right.equalTo(button);
        make.height.mas_equalTo(1);
    }];
    [button setTitleColor:RGBColorAlpha(255, 66, 89, 1) forState:UIControlStateNormal];
    
}
#pragma mark - 标尺代理方法

- (void)rulerWith:(NSInteger)days  andRulerType:(RulerType)_rulerType andView:(UIView *)view{
    
    int index = 0;
    RulerShowView * tmpView = (RulerShowView*)view.superview;
    if (_rulerType == RulerTypeForSize) {
        NSArray * array = [NSArray arrayWithObjects:@"155/82",@"160/66",@"160/75",@"160/80",@"160/84",@"160/86",@"165/90",@"165/94",@"165/98",@"170/90",@"170/92",@"170/96",@"180/100", nil];
        index = days/10.0;
        tmpView.valueText.text = [NSString stringWithFormat:@"%@",[array objectAtIndex:index]];
        //        [view reloadData:view.valueText.text];
    }else{
        tmpView.valueText.text = [NSString stringWithFormat:@"%ld",days];
        //        [view reloadData:[NSString stringWithFormat:@"%ld",days]];
        //即时打印出标尺滑动位置的数值
        NSLog(@"当前刻度值：%ld",days);
    }
    
}

- (void)rulerRunEnd {
    NSLog(@"end");
}

#pragma mark - textFeildDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    int index = (int)textField.tag-Text_Tag;
    if (textField.becomeFirstResponder) {
        UITextField * text = [self viewWithTag:Text_Tag+index+1];
        [textField resignFirstResponder];
        [text becomeFirstResponder];
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
    
    return YES;
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    
    return YES;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
